package br.urca.impl;

import java.util.List;

import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.transaction.Transactional;

import br.urca.dao.CursoTurnoDAO;
import br.urca.model.Curso;
import br.urca.model.CursoTurno;

@Transactional
public class JPACursoTurnoDAO implements CursoTurnoDAO {
	
	private EntityManager manager;
	
	public JPACursoTurnoDAO() {
		this(null);
	}
	
	@Inject
	public JPACursoTurnoDAO(EntityManager manager) {
		this.manager = manager;
	}

	@Override
	public List<CursoTurno> todos() {
		try {
			return this.manager.createQuery("SELECT c FROM CursoTurno c",
					CursoTurno.class).getResultList();
		} catch (Exception e) {
			return null;
		}
	}

	@Override
	public CursoTurno buscarPorCursoTurno(String codCursoTuno, Curso curso) {
		try {
			return this.manager
					.createQuery(
							"SELECT c FROM CursoTurno c "
									+ "WHERE c.codCursoTurno = :codCursoTurno "
									+ "AND c.curso = :curso ", CursoTurno.class)
					.setParameter("curso", curso)
					.setParameter("codCursoTurno", codCursoTuno)
					.getSingleResult();
		} catch (Exception e) {
			return null;
		}
	}

}
